#!/bin/bash

# postinst script for network-ipfilter
#
# see: dh_installdeb(1)

#set -e

# summary of how this script can be called:
#        * <postinst> \`configure' <most-recently-configured-version>
#        * <old-postinst> \`abort-upgrade' <new version>
#        * <conflictor's-postinst> \`abort-remove' \`in-favour' <package>
#          <new-version>
#        * <postinst> \`abort-remove'
#        * <deconfigured's-postinst> \`abort-deconfigure' \`in-favour'
#          <failed-install-package> <version> \`removing'
#          <conflicting-package> <version>
# for details, see http://www.debian.org/doc/debian-policy/ or
# the debian-policy package

PATH=/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin
export PATH

if [ "${BASEDIR:=/}" = "/" ]; then
    BASEDIR=""
else
    BASEDIR_OPT="-b $BASEDIR"
fi

case "$1" in
    configure)
        chown root:bin $BASEDIR/usr/share/man/man4i
	chown root:bin $BASEDIR/usr/share/man/man7
	chown root:bin $BASEDIR/usr/share/man/man8
	chown root:bin $BASEDIR/usr/lib/ipf
	chown root:bin $BASEDIR/usr/share/ipfilter
	chown root:bin $BASEDIR/usr/share/ipfilter/examples
	chown root:sys $BASEDIR/etc/ipf
	chown root:sys $BASEDIR/lib/svc/manifest/network
	chown root:sys $BASEDIR/var/db
	chown root:sys $BASEDIR/var/db/ipf
	chmod 0555 "$BASEDIR/lib/svc/method/ipfilter"
	chown root:bin "$BASEDIR/lib/svc/method/ipfilter"
	chmod 0555 "$BASEDIR/usr/lib/ipf/ipftest"
	chown root:bin "$BASEDIR/usr/lib/ipf/ipftest"
	chmod 0555 "$BASEDIR/usr/lib/servinfo"
	chown root:bin "$BASEDIR/usr/lib/servinfo"
	chmod 0555 "$BASEDIR/usr/sbin/ipf"
	chown root:bin "$BASEDIR/usr/sbin/ipf"
	chmod 0555 "$BASEDIR/usr/sbin/ipfs"
	chown root:bin "$BASEDIR/usr/sbin/ipfs"
	chmod 0555 "$BASEDIR/usr/sbin/ipfstat"
	chown root:bin "$BASEDIR/usr/sbin/ipfstat"
	chmod 0555 "$BASEDIR/usr/sbin/ipmon"
	chown root:bin "$BASEDIR/usr/sbin/ipmon"
	chmod 0555 "$BASEDIR/usr/sbin/ipnat"
	chown root:bin "$BASEDIR/usr/sbin/ipnat"
	chmod 0555 "$BASEDIR/usr/sbin/ippool"
	chown root:bin "$BASEDIR/usr/sbin/ippool"
	chmod 0644 "$BASEDIR/usr/lib/ipf/IPFILTER.LICENCE"
	chown root:bin "$BASEDIR/usr/lib/ipf/IPFILTER.LICENCE"
	chmod 0644 "$BASEDIR/usr/share/ipfilter/examples/BASIC.NAT"
	chown root:bin "$BASEDIR/usr/share/ipfilter/examples/BASIC.NAT"
	chmod 0644 "$BASEDIR/usr/share/ipfilter/examples/BASIC_1.FW"
	chown root:bin "$BASEDIR/usr/share/ipfilter/examples/BASIC_1.FW"
	chmod 0644 "$BASEDIR/usr/share/ipfilter/examples/BASIC_2.FW"
	chown root:bin "$BASEDIR/usr/share/ipfilter/examples/BASIC_2.FW"
	chmod 0644 "$BASEDIR/usr/share/ipfilter/examples/example.1"
	chown root:bin "$BASEDIR/usr/share/ipfilter/examples/example.1"
	chmod 0644 "$BASEDIR/usr/share/ipfilter/examples/example.10"
	chown root:bin "$BASEDIR/usr/share/ipfilter/examples/example.10"
	chmod 0644 "$BASEDIR/usr/share/ipfilter/examples/example.11"
	chown root:bin "$BASEDIR/usr/share/ipfilter/examples/example.11"
	chmod 0644 "$BASEDIR/usr/share/ipfilter/examples/example.12"
	chown root:bin "$BASEDIR/usr/share/ipfilter/examples/example.12"
	chmod 0644 "$BASEDIR/usr/share/ipfilter/examples/example.13"
	chown root:bin "$BASEDIR/usr/share/ipfilter/examples/example.13"
	chmod 0644 "$BASEDIR/usr/share/ipfilter/examples/example.2"
	chown root:bin "$BASEDIR/usr/share/ipfilter/examples/example.2"
	chmod 0644 "$BASEDIR/usr/share/ipfilter/examples/example.3"
	chown root:bin "$BASEDIR/usr/share/ipfilter/examples/example.3"
	chmod 0644 "$BASEDIR/usr/share/ipfilter/examples/example.4"
	chown root:bin "$BASEDIR/usr/share/ipfilter/examples/example.4"
	chmod 0644 "$BASEDIR/usr/share/ipfilter/examples/example.5"
	chown root:bin "$BASEDIR/usr/share/ipfilter/examples/example.5"
	chmod 0644 "$BASEDIR/usr/share/ipfilter/examples/example.6"
	chown root:bin "$BASEDIR/usr/share/ipfilter/examples/example.6"
	chmod 0644 "$BASEDIR/usr/share/ipfilter/examples/example.7"
	chown root:bin "$BASEDIR/usr/share/ipfilter/examples/example.7"
	chmod 0644 "$BASEDIR/usr/share/ipfilter/examples/example.8"
	chown root:bin "$BASEDIR/usr/share/ipfilter/examples/example.8"
	chmod 0644 "$BASEDIR/usr/share/ipfilter/examples/example.9"
	chown root:bin "$BASEDIR/usr/share/ipfilter/examples/example.9"
	chmod 0644 "$BASEDIR/usr/share/ipfilter/examples/example.sr"
	chown root:bin "$BASEDIR/usr/share/ipfilter/examples/example.sr"
	chmod 0644 "$BASEDIR/usr/share/ipfilter/examples/firewall"
	chown root:bin "$BASEDIR/usr/share/ipfilter/examples/firewall"
	chmod 0644 "$BASEDIR/usr/share/ipfilter/examples/ftp-proxy"
	chown root:bin "$BASEDIR/usr/share/ipfilter/examples/ftp-proxy"
	chmod 0644 "$BASEDIR/usr/share/ipfilter/examples/ftppxy"
	chown root:bin "$BASEDIR/usr/share/ipfilter/examples/ftppxy"
	chmod 0644 "$BASEDIR/usr/share/ipfilter/examples/ip_rules"
	chown root:bin "$BASEDIR/usr/share/ipfilter/examples/ip_rules"
	chmod 0644 "$BASEDIR/usr/share/ipfilter/examples/nat-setup"
	chown root:bin "$BASEDIR/usr/share/ipfilter/examples/nat-setup"
	chmod 0644 "$BASEDIR/usr/share/ipfilter/examples/nat.eg"
	chown root:bin "$BASEDIR/usr/share/ipfilter/examples/nat.eg"
	chmod 0644 "$BASEDIR/usr/share/ipfilter/examples/pool.conf"
	chown root:bin "$BASEDIR/usr/share/ipfilter/examples/pool.conf"
	chmod 0644 "$BASEDIR/usr/share/ipfilter/examples/server"
	chown root:bin "$BASEDIR/usr/share/ipfilter/examples/server"
	chmod 0644 "$BASEDIR/usr/share/ipfilter/examples/tcpstate"
	chown root:bin "$BASEDIR/usr/share/ipfilter/examples/tcpstate"
	chmod 0755 "$BASEDIR/usr/share/ipfilter/examples/mkfilters"
	chown root:bin "$BASEDIR/usr/share/ipfilter/examples/mkfilters"
	chmod 0444 "$BASEDIR/lib/svc/manifest/network/ipfilter.xml"
	chown root:sys "$BASEDIR/lib/svc/manifest/network/ipfilter.xml"
	[ -f $BASEDIR/etc/ipf/ipf.conf ] || cp -ax $BASEDIR/usr/share/doc/network-ipfilter/examples/ipf.conf $BASEDIR/etc/ipf/ipf.conf
	chmod 0644 "$BASEDIR/etc/ipf/ipf.conf"
	chown root:sys "$BASEDIR/etc/ipf/ipf.conf"
	if [ "$ZONEINST" = "1" ]; then
	    [ -f $BASEDIR/usr/kernel/drv/ipf.conf ] && rm -f $BASEDIR/usr/kernel/drv/ipf.conf
	else
	    chmod 0644 "$BASEDIR/usr/kernel/drv/ipf.conf"
	    chown root:sys "$BASEDIR/usr/kernel/drv/ipf.conf"
	fi
	if [ "$ZONEINST" = "1" ]; then
	    [ -f $BASEDIR/usr/kernel/drv/amd64/ipf ] && rm -f $BASEDIR/usr/kernel/drv/amd64/ipf
	else
	    chmod 0755 "$BASEDIR/usr/kernel/drv/amd64/ipf"
	    chown root:sys "$BASEDIR/usr/kernel/drv/amd64/ipf"
	fi
	[ "$ZONEINST" = "1" ] || (grep -c "^ipf " $BASEDIR/etc/name_to_major >/dev/null || ( add_drv -n  $BASEDIR_OPT  -m '* 0666 root sys' -p 'read_priv_set=sys_ip_config write_priv_set=sys_ip_config' ipf ) )
	
    ;;

    abort-upgrade|abort-remove|abort-deconfigure)
    ;;

    *)
        echo "postinst called with unknown argument '$1'" >&2
        exit 1
    ;;
esac



# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

#DEBHELPER#

exit 0

